import datetime

from PySide6.QtCore import QSize, Qt, QTime
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QWidget, QStyle, QVBoxLayout, QDialogButtonBox, QFormLayout, QLabel, QLineEdit, \
    QDateTimeEdit, QHBoxLayout, QCheckBox, QSpinBox, QTimeEdit, QMessageBox, QComboBox

from AccountManager import AccountManager


class NewWindow(QWidget):
    def __init__(self, parent, account_manager: AccountManager, res_x: int, res_y: int, dbaccessor):
        super().__init__()
        self.Account_Manager = account_manager
        self.RES_X = res_x
        self.RES_Y = res_y
        self.DBAccessor = dbaccessor
        self.parent = parent

        # 初始化参数
        # self.TitlePointSize = int(self.RES_Y / 75)
        # temp_font = QFont("微软雅黑", self.TitlePointSize)
        # temp_font.setBold(True)

        # 设置窗口属性
        self.setWindowIcon(self.style().standardPixmap(QStyle.StandardPixmap.SP_TrashIcon))  # 图标
        self.setWindowTitle("Magic Calendar: New Schedule (*^▽^*)")
        self.setFixedSize(QSize(int(self.RES_X / 5), int(self.RES_Y / 4.7)))  # 最小窗口大小

        # 设置主布局
        self.layout_f_main = QFormLayout(self)

        # 输入参数
        self.label_info_sname_title = QLabel("日程标题：")  # 日程标题
        self.lineedit_info_sname_content = QLineEdit()
        self.layout_f_main.addRow(self.label_info_sname_title, self.lineedit_info_sname_content)

        self.label_info_starttime_title = QLabel("开始时间：")  # 开始时间
        self.dtedit_info_starttime_content = QDateTimeEdit()
        self.dtedit_info_starttime_content.setDateTime(datetime.datetime.now())
        self.layout_f_main.addRow(self.label_info_starttime_title, self.dtedit_info_starttime_content)

        self.label_info_endtime_title = QLabel("结束时间：")  # 结束时间
        self.dtedit_info_endtime_content = QDateTimeEdit()
        self.dtedit_info_endtime_content.setDateTime(datetime.datetime.now())
        self.layout_f_main.addRow(self.label_info_endtime_title, self.dtedit_info_endtime_content)

        self.label_info_setting_title = QLabel("其他设置：")  # 其他设置
        self.layout_h_setting = QHBoxLayout()
        self.checkbox_info_abs = QCheckBox("忽略结束时间")
        self.checkbox_info_abs.clicked.connect(self.check_abs)
        self.layout_h_setting.addWidget(self.checkbox_info_abs)
        self.checkbox_info_cycle = QCheckBox("启用循环")
        self.checkbox_info_cycle.clicked.connect(self.check_cycle)
        self.layout_h_setting.addWidget(self.checkbox_info_cycle)
        self.layout_f_main.addRow(self.label_info_setting_title, self.layout_h_setting)

        self.label_info_cycle_title = QLabel("循环时间")  # 循环时间
        self.layout_h_cycle = QHBoxLayout()
        self.spin_info_cycle_day_content = QSpinBox()
        self.spin_info_cycle_day_content.setReadOnly(True)
        self.layout_h_cycle.addWidget(self.spin_info_cycle_day_content)
        self.label_info_cycle_day_title = QLabel("天")
        self.layout_h_cycle.addWidget(self.label_info_cycle_day_title)
        self.spin_info_cycle_time_content = QTimeEdit()
        self.spin_info_cycle_time_content.setReadOnly(True)
        self.layout_h_cycle.addWidget(self.spin_info_cycle_time_content)
        self.label_info_cycle_time_title = QLabel("时分")
        self.layout_h_cycle.addWidget(self.label_info_cycle_time_title)
        self.layout_f_main.addRow(self.label_info_cycle_title, self.layout_h_cycle)

        self.label_info_description_title = QLabel("日程描述：")  # 日程描述
        self.lineedit_info_description_content = QLineEdit()
        self.layout_f_main.addRow(self.label_info_description_title, self.lineedit_info_description_content)

        self.label_info_target_title = QLabel("发布对象：")  # 日程描述
        self.combo_info_target_content = QComboBox()
        self.combo_info_target_content.addItem(self.Account_Manager.nickname)
        self.layout_f_main.addRow(self.label_info_target_title, self.combo_info_target_content)

        # 设置按钮
        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.layout_f_main.addWidget(self.button_box)
        self.button_box.button(QDialogButtonBox.Ok).clicked.connect(self.confirm)
        self.button_box.button(QDialogButtonBox.Cancel).clicked.connect(self.cancel)

    def confirm(self):
        if self.lineedit_info_sname_content.text() == '':
            QMessageBox.information(self, "出现了错误！", "标题不能为空！", QMessageBox.StandardButton.Ok)
            return
        if self.dtedit_info_starttime_content.dateTime() > self.dtedit_info_endtime_content.dateTime():
            QMessageBox.information(self, "出现了错误！", "开始时间不能大于结束时间！", QMessageBox.StandardButton.Ok)
            return
        if self.checkbox_info_cycle.isChecked():
            if self.spin_info_cycle_day_content.value() == 0 and self.spin_info_cycle_time_content.time() == QTime(0,
                                                                                                                   0):
                QMessageBox.information(self, "出现了错误！", "循环时间不能为0！", QMessageBox.StandardButton.Ok)
                return
        sname = self.lineedit_info_sname_content.text()
        starttime = datetime.datetime(self.dtedit_info_starttime_content.date().year(),
                                      self.dtedit_info_starttime_content.date().month(),
                                      self.dtedit_info_starttime_content.date().day(),
                                      self.dtedit_info_starttime_content.time().hour(),
                                      self.dtedit_info_starttime_content.time().minute(),
                                      self.dtedit_info_starttime_content.time().second())
        if self.checkbox_info_abs.isChecked():
            endtime = starttime
        else:
            endtime = datetime.datetime(self.dtedit_info_endtime_content.date().year(),
                                        self.dtedit_info_endtime_content.date().month(),
                                        self.dtedit_info_endtime_content.date().day(),
                                        self.dtedit_info_endtime_content.time().hour(),
                                        self.dtedit_info_endtime_content.time().minute(),
                                        self.dtedit_info_endtime_content.time().second())
        if self.checkbox_info_cycle.isChecked():
            cycle = str(self.spin_info_cycle_day_content.value()) + " " + str(
                self.spin_info_cycle_time_content.time().hour()) + " " + str(
                self.spin_info_cycle_time_content.time().minute())
        else:
            cycle = None
        description = self.lineedit_info_description_content.text()
        uid_pub = self.Account_Manager.uid
        nickname = self.combo_info_target_content.currentText()
        uid = self.DBAccessor.select_user(nickname=nickname)[0]
        self.DBAccessor.insert_schedule(sname, starttime, endtime, cycle, uid, uid_pub, description)
        self.parent.refresh_buttons()
        self.close()

    def cancel(self):
        self.close()

    def check_abs(self):
        if self.checkbox_info_abs.isChecked():
            self.dtedit_info_endtime_content.setReadOnly(True)
            self.dtedit_info_endtime_content.setDateTime(self.dtedit_info_starttime_content.dateTime())
        else:
            self.dtedit_info_endtime_content.setReadOnly(False)

    def check_cycle(self):
        if self.checkbox_info_cycle.isChecked():
            self.spin_info_cycle_day_content.setReadOnly(False)
            self.spin_info_cycle_time_content.setReadOnly(False)
        else:
            self.spin_info_cycle_day_content.setReadOnly(True)
            self.spin_info_cycle_time_content.setReadOnly(True)
            self.spin_info_cycle_day_content.setValue(0)
            self.spin_info_cycle_time_content.setTime(QTime(0, 0))